package com.jgdt.demo.module;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.widget.Toast;

/**
 * Time:2019/12/30
 * Author:
 * Description:
 */
public class LocationTools {
    private static String[] providers=new String[]{LocationManager.NETWORK_PROVIDER,LocationManager.GPS_PROVIDER};
    private static Location newLocation(String locationType, double longitude, double latitude) {
        Location loc = new Location(locationType);
        loc.setAccuracy(2.0F);
        loc.setAltitude(55.0D);
        loc.setBearing(1.0F);
        Bundle bundle = new Bundle();
        bundle.putInt("satellites", 7);
        loc.setExtras(bundle);
        loc.setLongitude(longitude);//经度
        loc.setLatitude(latitude);//纬度
        loc.setTime(System.currentTimeMillis());
        if (Build.VERSION.SDK_INT >= 17) {
            loc.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        }
        return loc;
    }
    //模拟位置-启用，坐标点格式：[经度，纬度]
    public static boolean enableMockLocation(Context ctx, double longitude, double latitude) {
        try {
            boolean canMockPosition = false;
            if (Build.VERSION.SDK_INT <= 22) {//6.0以下
                canMockPosition = Settings.Secure.getInt(ctx.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0;
            } else {
                LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);//获得LocationManager引用
                for(String locationType:providers){
                    //locationManager.setTestProviderEnabled(locationType, false);
                    locationManager.removeTestProvider(locationType);
                    LocationProvider provider = locationManager.getProvider(locationType);
                    if (provider != null) {
                        locationManager.addTestProvider(
                                provider.getName()
                                , provider.requiresNetwork()
                                , provider.requiresSatellite()
                                , provider.requiresCell()
                                , provider.hasMonetaryCost()
                                , provider.supportsAltitude()
                                , provider.supportsSpeed()
                                , provider.supportsBearing()
                                , provider.getPowerRequirement()
                                , provider.getAccuracy());
                    } else {
                        locationManager.addTestProvider(
                                locationType
                                , true, true, false, false, true, true, true
                                , Criteria.POWER_HIGH, Criteria.ACCURACY_FINE);
                    }
                    // 模拟位置可用
                    locationManager.setTestProviderEnabled(locationType, true);
                    locationManager.setTestProviderStatus(locationType, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
                    //设置坐标
                    locationManager.setTestProviderLocation(locationType, newLocation(locationType,longitude,latitude));
                }
                canMockPosition = true;
            }
            Toast.makeText(ctx, "SUCCESS：模拟位置坐标已设置["+longitude+", "+latitude+"]", Toast.LENGTH_LONG).show();
            return canMockPosition;
        } catch (Exception e) {
            Toast.makeText(ctx, "ERROR：" + e.getMessage(), Toast.LENGTH_LONG).show();
            return false;
        }
    }
    //模拟位置-禁用
    public static void  disableMockLocation(Context ctx) {
        try {
            if (Build.VERSION.SDK_INT > 22) {
                LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);//获得LocationManager引用
                for(String locationType:providers){
                    locationManager.setTestProviderEnabled(locationType, false);
                    locationManager.removeTestProvider(locationType);
                }
            }
            Toast.makeText(ctx, "SUCCESS：模拟位置功能-已禁用", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(ctx, "ERROR：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}